<?php
require_once("../../../config.inc.php");
require_once("../../../app/_inc/include.php");

$errMsg = sxREQ('msg');
if(!isset($_REQUEST["mod"])){
	$url = $_SESSION[USEROBJ]->getHistory(-1, true)."&msg=".urlencode(_t("variabile 'mod' non passata")); 
	serverRedir($url);
}
if(!isset($_REQUEST["menu_page"])){
	$url = $_SESSION[USEROBJ]->getHistory(-1, true)."&msg=".urlencode(_t("variabile 'menu_page' non passata")); 
	serverRedir($url);
}
if(isset($_REQUEST["del"]) and $_REQUEST["del"]) {
	$errMsg = menuPageDel();
}
$module = sxREQ('mod');
$menuPage = sxREQ('menu_page');
$sql = "SELECT mp.*, ml.menu_group, ml.menu_line, mg.sequence AS gseq, ml.sequence AS lseq, " .
		"    mg.flag_display, mg.description AS gdes, ml.description AS ldes," .
		"    ml.linked_module, ml.linked_page, ml.process_unit, ml.query_string, m.description AS mdes " .
		" FROM menu_page mp" .
		" INNER JOIN module m ON m.module = mp.module " .
		" LEFT JOIN menu_line ml ON ml.module = mp.module AND ml.menu_page = mp.menu_page " .
		" LEFT JOIN menu_group mg ON mg.module = mp.module AND mg.menu_page = mp.menu_page " .
		"    AND mg.menu_group = ml.menu_group " .
		" WHERE mp.module = '$module' AND mp.menu_page = '$menuPage' " .
		" ORDER BY mg.sequence, ml.sequence ";
$query = $objSDAO->query($sql, "mp");
$row = $objSDAO->fetch("mp");
if(!$row){
	$url = $_SESSION[USEROBJ]->getHistory(-1, true)."&msg=".urlencode(_t("pagina men@ '%s' inesistente",$menuPage)); 
	serverRedir($url);
}

//output HTML
$aOpz = array("kMenu"=>'pro.mod');
echo(headerHTML($aOpz));
?>
<script language="JavaScript" type="text/javascript">
	function confDelMenuPage(){
		try{
			if (confirm("Confirm ?")) {
				document.location.href = "menuPageView.php?del=yes&<?="mod=$module&menu_page=$menuPage&xsn=".XSN?>";
			}
		}catch(e){
			raiseError(e.description, "???");
		}			
	}
</script>
  
<?php
echo(bodyHTML($aOpz));
echo("<table width=\"100%\" cellpadding=\"03\" cellspacing=\"0\">\n");
echo("<tr>" .
			"<td class=\"labl01\" width='10%'>module</td>".
			"<td class=\"text01\" width='90%'>$module - ".$row['mdes']."</td>" .
		"</tr>\n</table>");
$aCmd = array(
					"lisgl"=>array("pag"=>"menuPageGroupList.php?mod=$module&menu_page=$menuPage",
										"title"=>_t("menu line group"),
										"text"=>_t("menu line group"),
										"align"=>"L"
										),
					"newmp"=>array("pag"=>"menuPageLineIns.php?mod=$module&menu_page=$menuPage",
										"title"=>_t("new menu line"),
										"text"=>_t("new menu line")
										),
					"delMP"=>array("pag"=>"javascript:confDelMenuPage();",
										"title"=>_t("delete menu page"),
										"text"=>_t("delete")
										),
					"modMP"=>array("pag"=>"menuPageMod.php?mod=$module&menu_page=$menuPage",
										"title"=>_t("change menu page"),
										"text"=>_t("change")
										),
					"back"=>array("pag"=>$objUser->getHistory(-1,false),
										"title"=>$objUser->getHistoryText(-1),
										"text"=>$objUser->getHistoryText(-1)
										)
					);
echo(commandsHTML($aCmd));	
echo("<table class=\"tabl01\" width=\"100%\" cellpadding=\"3\" cellspacing=\"2\" border='0'>\n");
echo("<tr>" .
			"<td class=\"titl01\" colspan='2'>"._t("Menu page").": $menuPage - ".$row['description'].
			(($row['start_page']=='Y')?' - ('._t('iniziale').')':'')."</td>" .
		"</tr>\n");
echo("<tr>" .
			"<td class=\"labl01\" width='20%' valign='top'>"._t("Note")."</td>" .
			"<td class=\"text01\"width='80%' valign='top'><pre class=\"text01\" >".sxWW($row['note'])."</pre></td>" .
		"</tr>\n");
echo("</table>");
echo("<table class=\"tabl01\" width=\"100%\" cellpadding=\"0\" cellspacing=\"1\" border='0'><tr><td>\n");
echo("<table class=\"tabl01\" width=\"100%\" cellpadding=\"3\" cellspacing=\"1\" border='0'>\n");
echo("<tr>" .
			"<td class=\"menu01\" colspan='4' align='center'>".$row['description']."</td>" .
		"</tr>\n");
$nLine = 0;
$nMaxLine = 16;
$aMenuLine = array();
$menuGroup = '???';
while ($row) {
	if (!empty($row['menu_line']))	{
		if ($menuGroup != $row['menu_group']) {
			if (!empty($row['flag_display']) and $row['flag_display'] == 'Y') {
				$aMenuLine[] = array("isGroup"=>true,"menu_group"=>$row['menu_group'],
					"gdes"=>$row['gdes'],"gseq"=>$row['gseq']);
			}
			$menuGroup = $row['menu_group'];
		} 
		$aMenuLine[] = $row;
	}
	$row = $objSDAO->fetch("mp");
}
if (count($aMenuLine) > $nMaxLine*2) {
	$nMaxLine = ceil(count($aMenuLine)/2);
} elseif (count($aMenuLine) < $nMaxLine) {
	$nMaxLine = count($aMenuLine);
}
$nVoce = $nIndG = 1;
$subVoce = 0;
for($nLine=0; $nLine<$nMaxLine; $nLine++) {
	$row = $aMenuLine[$nLine];
	echo("<tr>");
	echoLine($row,'L');
	$nLine2 = $nLine + $nMaxLine;
	if(isset($aMenuLine[$nLine2])) { 
		$row = $aMenuLine[$nLine2];
		echoLine($row,'R');
		echo("</tr>\n");
	} else {
		echo("<td class=\"menu02\"  width='50%' colspan='2'>&nbsp;</td>" .
			"</tr>\n");
	}
}
function echoLine($row,$lato) {
	global $module, $menuPage, $nVoce, $nIndG, $subVoce;
	$aLet = "abcdefghijklmnopqrstuvxywz";
	if (isset($row['isGroup'])) {
		$sC1 = "<td class=\"text01\" width='1%'>".$row['menu_group'].':'.$row['gseq']."/g</td>";		
		$nIndG = $nVoce;
		$nVoce++;	
		$subVoce = 0;
		$sC2 = "<td class=\"menu02\" width='49%'>" .
				"$nIndG. <a class='menu02' href=\"javascript:pageRedir('menuPageGroupView.php?".
				"mod=".$module."&menu_page=".$menuPage."&menu_group=".$row['menu_group']."&xsn=".XSN."');\" >".
				"<b>".$row['gdes']."</b></a></td>";	
	} else {
		$sC1 = "<td class=\"text01\" width='1%'>".
			substr($row['menu_line'],10).":".(($row['gseq']=='')?'-':$row['gseq']).'/'.$row['lseq']."</td>";		
		if ($row['flag_display'] == 'Y') {
			$class = 'menu03';
			$indent = "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
			$nInd = $nIndG.$aLet[$subVoce];
			$subVoce++;
		} else {
			$class = 'menu02';
			$indent = "";
			$nInd = $nVoce;
			$nVoce++;
		}
		$sC2 = "<td class=\"$class\" width='49%'>$indent" .$nInd.
				". <a class='$class' href=\"javascript:pageRedir('menuPageLineView.php?".
				"menu_line=".$row['menu_line']."&xsn=".XSN."');\" >".
				$row['ldes']."</span></td>";
	}
	if ($lato == 'L') echo $sC1.$sC2;
	else echo $sC2.$sC1;
}
echo("</table>");
echo("</td></tr></table>");
echo(commandsHTML($aCmd));
echo("</td>\n</tr>\n</table>\n");
echo("</form>");
echo(footerHTML());
exit();

function menuPageDel() {
	global $objSDAO;
	$errMsg = "";
	$sSQL = "DELETE FROM menu_line " .
			" WHERE module='".sxRAS("mod")."' " .
					" AND menu_page = '".sxRAS("menu_page")."'";
	$objSDAO->exec($sSQL);
	$sSQL = "DELETE FROM menu_group " .
			" WHERE module='".sxRAS("mod")."' " .
					" AND menu_page = '".sxRAS("menu_page")."'";
	$objSDAO->exec($sSQL);
	$sSQL = "DELETE FROM menu_page " .
			" WHERE module='".sxRAS("mod")."' " .
					" AND menu_page = '".sxRAS("menu_page")."'";
	$objSDAO->exec($sSQL);
	logRecord("300", _t("Eliminreqtype pagina men@ '".sxREQUEST('module')."/".sxREQUEST('menu_page')."'"));
	//serverRedir("moduleList.php?xsn=".XSN);
	$url = $_SESSION[USEROBJ]->getHistory(-1, true);
	serverRedir($url);
	exit;
}

?>
